package com.blockmeta.bbs.immodule.adapter;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.net.callback.BaseResponseCallBack;
import com.blockmeta.bbs.baselibrary.utils.LoadPicUtil;
import com.blockmeta.bbs.baselibrary.utils.RegexUtil;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.util.RouterActivityHelper;
import com.blockmeta.bbs.businesslibrary.widget.CircleGradeAndKind;
import com.blockmeta.bbs.businesslibrary.widget.CircleImageView;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.activity.IMSearchCircleResultActivity;
import com.blockmeta.bbs.immodule.activity.IMSearchFriendsResultActivity;
import com.blockmeta.bbs.immodule.activity.chat.CustomerServiceChatActivity;
import com.blockmeta.bbs.immodule.activity.chat.UCGChatActivity;
import com.blockmeta.bbs.immodule.activity.chat.UUChatActivity;
import com.blockmeta.bbs.immodule.bean.IMCommonLib;
import com.blockmeta.bbs.immodule.bean.IMSearchNormalPOJO;
import com.blockmeta.bbs.immodule.bean.Params;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.dao.ChatMessage;
import com.blockmeta.bbs.immodule.dao.IMUserEntity;
import com.blockmeta.bbs.immodule.utils.ImTimeUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * @author AirThor
 * @date 2020/5/12.
 */
public class IMSearchNormalResultAdapter extends BaseMultiItemQuickAdapter<IMSearchNormalPOJO,BaseViewHolder> {

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public IMSearchNormalResultAdapter(List<IMSearchNormalPOJO> data) {
        super(data);
        addItemType(IMSearchNormalPOJO.CIRCLE,R.layout.item_im_search_circle);
        addItemType(IMSearchNormalPOJO.FRIEND, R.layout.item_im_search_friends);
        addItemType(IMSearchNormalPOJO.SESSION,R.layout.item_im_search_session);
        addItemType(IMSearchNormalPOJO.DIVIDER,R.layout.item_im_search_divider);
        addItemType(IMSearchNormalPOJO.MORE,R.layout.item_im_search_more);
    }

    @Override
    public int getItemViewType(int position) {
        Log.d(TAG, "getItemViewType: "+position);
        return super.getItemViewType(position);
    }

    @Override
    protected void convert(BaseViewHolder helper, IMSearchNormalPOJO item) {
        switch (helper.getItemViewType()){
            case IMSearchNormalPOJO.CIRCLE:
                circle(helper,item);
                break;
            case IMSearchNormalPOJO.FRIEND:
                friend(helper,item);
                break;
            case IMSearchNormalPOJO.SESSION:
                session(helper,item);
                break;
            case IMSearchNormalPOJO.DIVIDER:
                dividers(helper,item);
                break;
            case IMSearchNormalPOJO.MORE:
                more(helper,item);
                break;
        }
    }

    private void circle(BaseViewHolder helper, final IMSearchNormalPOJO item) {
        //为按钮绑定点击事件
        ImageView ivLogo = helper.getView(R.id.iv_avatar);
        LoadPicUtil.loadForIm(ivLogo,item.avator);

        helper.setText(R.id.tv_circle_name, RegexUtil.getHightLight(item.name,item.keyWord));

        helper.setText(R.id.tv_circle_tag,item.circleKind);

        helper.setText(R.id.tv_circle_size,item.circleSize);

        CircleGradeAndKind circleGradeAndKind = helper.getView(R.id.cgk_info);
        circleGradeAndKind.setText(item.circleGrade+"","群组");

        helper.getView(R.id.item_click).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, UCGChatActivity.class);
                intent.putExtra(Params.USER_TAG, item.id);
                mContext.startActivity(intent);
            }
        });
    }


    private void friend(BaseViewHolder helper, final IMSearchNormalPOJO item) {
        CircleImageView avatar = helper.getView(R.id.contacts_avatar);
        LoadPicUtil.loadWithPicasso(avatar, item.avator);
        helper.getView(R.id.contacts_avatar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterActivityHelper.openUserCenter(mContext, item.id + "");
            }
        });

        helper.setText(R.id.contacts_name, RegexUtil.getHightLight(item.name,item.keyWord));

        helper.setText(R.id.contacts_grade,item.peopleGrade);

        helper.setText(R.id.contacts_registration_time, "加入 "+item.boatAge);

        helper.getView(R.id.item_click).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, UUChatActivity.class);
                intent.putExtra(Params.USER_TAG, item.id);
                mContext.startActivity(intent);
            }
        });
    }


    private void session(BaseViewHolder helper, IMSearchNormalPOJO item) {
        ChatMessage chatMessage = item.mChatMessage;
        int msgType = chatMessage.getMsgType();

        switch (msgType) {
            case ChatMessage.MSG_TYPE_UU:
                //人人聊天
                initUUView(helper, chatMessage,item.keyWord);
                break;
            case ChatMessage.MSG_TYPE_UCG:
                //群聊
                initUCGView(helper, chatMessage,item.keyWord);
                break;
        }
    }


    private void dividers(BaseViewHolder helper, IMSearchNormalPOJO item) {
        helper.setText(R.id.tv_divider_name,item.dividerTitle);
    }

    private void more(BaseViewHolder helper, final IMSearchNormalPOJO item) {
        helper.getView(R.id.tv_more).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onMoreClicked(item);
            }
        });

    }

    private void onMoreClicked(IMSearchNormalPOJO item) {
        if (item.whichHasMore ==IMSearchNormalPOJO.CIRCLE){
            showMoreData(IMSearchCircleResultActivity.class,item);
        }else {
            showMoreData(IMSearchFriendsResultActivity.class,item);
        }
    }

    private void showMoreData(Class<?> cls,IMSearchNormalPOJO item) {
        Intent intent = new Intent(mContext, cls);
        intent.putExtra(Constant.ZERO,item.keyWord);
        mContext.startActivity(intent);
    }

    //单聊
    private void initUUView(final BaseViewHolder helper, final ChatMessage item, final String keyword) {
        final ImageView icon = (ImageView) helper.getView(R.id.talk_list_icon);

        long userId = item.getFromId();
        switch (item.getType()) {
            case ChatMessage.TYPE_RECEIVE:
                userId = item.getFromId();
                break;
            case ChatMessage.TYPE_SEND:
                userId = item.getToId();
                break;
            default:
                break;
        }
        final TextView contentTv = helper.getView(R.id.talk_list_content);
        IMApplicationContext.getUserDomainService().getUser(userId, false, new BaseResponseCallBack<String, IMUserEntity>() {
            @Override
            public void fail(String msg) {
            }

            @Override
            public void success(IMUserEntity IMUserEntity) {
                LoadPicUtil.loadWithPicasso(icon, IMUserEntity.getAvatarPath());
                helper.setText(R.id.talk_list_name, TextUtils.isEmpty(IMUserEntity.getRemarkName()) ? IMUserEntity.getName() : IMUserEntity.getRemarkName());
                contentTv.setText(RegexUtil
                        .getHightLight(IMCommonLib.getContentNoSpan(IMApplicationContext
                        .getChatMessageDomainService().getShowContent(IMUserEntity, item)),keyword));
            }
        });
        helper.setText(R.id.talk_list_time, ImTimeUtils.SessionListTime(item.getDate()));

        final long finalUserId = userId;
        helper.getView(R.id.talk_list_main).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IMApplicationContext.getUserDomainService().getUser(finalUserId, false, new BaseResponseCallBack<String, IMUserEntity>() {
                    @Override
                    public void fail(String msg) {
                    }

                    @Override
                    public void success(IMUserEntity IMUserEntity) {
                        if (IMUserEntity.isCustomerService()) {
                            Intent intent = new Intent(mContext, CustomerServiceChatActivity.class);
                            intent.putExtra(Params.USER_TAG, finalUserId);
                            intent.putExtra(Params.MSGID, item.getId().longValue());
                            mContext.startActivity(intent);
                        } else {
                            Intent intent = new Intent(mContext, UUChatActivity.class);
                            intent.putExtra(Params.USER_TAG, finalUserId);
                            intent.putExtra(Params.MSGID, item.getId().longValue());
                            mContext.startActivity(intent);
                        }

                    }
                });
            }
        });

    }


    //群聊
    private void initUCGView(final BaseViewHolder helper, final ChatMessage item, final String keyword) {
        final CircleGradeAndKind gradeAndKind = helper.getView(R.id.talk_list_group);
        gradeAndKind.setVisibility(View.VISIBLE);

        final ImageView icon = helper.getView(R.id.talk_list_icon);

        final long chatGroupId = item.getChatGroupId();
        IMApplicationContext.getCircleDomainService().getGroup(chatGroupId, new BaseResponseCallBack<String, IMUserEntity>() {
            @Override
            public void fail(String msg) {
            }
            @Override
            public void success(IMUserEntity userEntity) {
                LoadPicUtil.loadForIm(icon, userEntity.getAvatarPath());

                helper.setText(R.id.talk_list_name, userEntity.getName());
                helper.setGone(R.id.iv_im_sign, userEntity.isIdentification());
                gradeAndKind.setText(userEntity.getGroupLevel(), "群组");
            }
        });

        helper.setText(R.id.talk_list_time, ImTimeUtils.SessionListTime(item.getDate()));
        final TextView contentTv = helper.getView(R.id.talk_list_content);

        if (item.getContentType() == ChatMessage.CONTENT_TYPE_NOTIFY) {
            //系统提示
            IMCommonLib.fillContentNoSpan(contentTv
                    , IMApplicationContext.getChatMessageDomainService().getShowContent(null, item));
        } else {
            IMApplicationContext.getUserDomainService().getUser(item.getFromId(), false, new BaseResponseCallBack<String, IMUserEntity>() {
                @Override
                public void fail(String msg) {
                }

                @Override
                public void success(IMUserEntity iMUserEntity) {
                    contentTv.setText(RegexUtil
                            .getHightLight(IMCommonLib.getContentNoSpan(IMApplicationContext.getChatMessageDomainService()
                            .getShowContent(iMUserEntity, item)),keyword));
                }
            });
        }

        //group ID
        helper.getView(R.id.talk_list_main).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, UCGChatActivity.class);
                intent.putExtra(Params.USER_TAG, chatGroupId);
                intent.putExtra(Params.MSGID, item.getId().longValue());
                mContext.startActivity(intent);
            }
        });

    }
}
